package com.example.city_home_app.RecyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.city_home_app.Activity.BaseActivity;
import com.example.city_home_app.Activity.NewsReplyActivity;
import com.example.city_home_app.Activity.UserInfoActivity;
import com.example.city_home_app.Bean.Down_NewsComment;
import com.example.city_home_app.Bean.Down_NewsReply;
import com.example.city_home_app.Bean.Down_Universe;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.MyCircleImageView;
import com.example.city_home_app.UI.UIUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.shaohui.bottomdialog.BottomDialog;

public class Adapter_NewsComment extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Down_NewsComment> list;
    private List<Integer> flag;
    private Context context;
    private ApiModel model;
    private android.support.v4.app.FragmentManager fragmentManager;
    private BottomDialog dialog;
    private int newsid;

    public Adapter_NewsComment(Context context, FragmentManager fragmentManager) {
        this.context = context;
        this.fragmentManager = fragmentManager;
        model = new ApiModel();
    }

    public void setList(List<Down_NewsComment> list) {
        this.list = list;
        flag = new ArrayList<>();
    }

    public void setNewsid(int newsid) {
        this.newsid = newsid;
    }

    public void clearList() {
        if (list != null) {
            this.list.clear();
        }
    }


    public void reverseList() {
        Collections.reverse(list);
        flag.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.recitem_news_comment, parent, false);
        Adapter_NewsComment.ViewHolder viewHolder = new Adapter_NewsComment.ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final Down_NewsComment bean = list.get(position);
        if (bean == null) {
            return;
        }
        final Adapter_NewsComment.ViewHolder viewHolder = (Adapter_NewsComment.ViewHolder) holder;
        if (bean.getComment_status() == 2 || bean.getComment_status() == 3) {
            viewHolder.tvRecitemNewcommentname.setText(bean.getUser_name());
            viewHolder.tvRecitemNewcommentcontent.setText("此评论已被删除");
            viewHolder.tvRecitemNewcommentcontent.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
            viewHolder.tvRecitemNewcommentcontent.getPaint().setAntiAlias(true);
            viewHolder.tvRecitemNewcommenttime.setText(bean.getComment_time());
            viewHolder.imgRecitemNewcommentLike.setVisibility(View.GONE);
            viewHolder.imgRecitemNewcommentComment.setVisibility(View.GONE);
            viewHolder.tvRecitemNewcommentlike.setVisibility(View.GONE);
            viewHolder.layoutRecitemNewcommentReply1.setVisibility(View.GONE);
            viewHolder.layoutRecitemNewcommentReply2.setVisibility(View.GONE);
            viewHolder.tvRecitemNewcommentAllreply.setVisibility(View.GONE);
            viewHolder.llRecitemBottomline.setVisibility(View.GONE);
            Picasso.with(context)
                    .load("http://siminzhang.top:8088/city_home_server/public/static/posters/" + bean.getUser_headimage())
                    .into(viewHolder.imgRecitemNewcomment);
            return;
        }
        viewHolder.tvRecitemNewcommentcontent.getPaint().setFlags(0);
        viewHolder.tvRecitemNewcommentname.setText(bean.getUser_name());
        viewHolder.tvRecitemNewcommentcontent.setText(bean.getComment_content());
        viewHolder.imgRecitemNewcommentLike.setVisibility(View.VISIBLE);
        viewHolder.imgRecitemNewcommentComment.setVisibility(View.VISIBLE);
        if (bean.getComment_like() == 0) {
            viewHolder.tvRecitemNewcommentlike.setVisibility(View.INVISIBLE);
        }
        viewHolder.tvRecitemNewcommentlike.setText("" + bean.getComment_like());
        viewHolder.tvRecitemNewcommenttime.setText(bean.getComment_time());

        Picasso.with(context)
                .load("http://siminzhang.top:8088/city_home_server/public/static/posters/" + bean.getUser_headimage())
                .into(viewHolder.imgRecitemNewcomment);
        RetrofitListener<List<Down_NewsReply>> listRetrofitListener = new RetrofitListener<List<Down_NewsReply>>() {
            @Override
            public void onResponse(final List<Down_NewsReply> listener) {
                if (listener.size() == 0) {
                    viewHolder.layoutRecitemNewcommentReply1.setVisibility(View.GONE);
                    viewHolder.layoutRecitemNewcommentReply2.setVisibility(View.GONE);
                    viewHolder.tvRecitemNewcommentAllreply.setVisibility(View.GONE);
                    viewHolder.llRecitemBottomline.setVisibility(View.GONE);
                } else if (listener.size() == 1) {
                    viewHolder.layoutRecitemNewcommentReply1.setVisibility(View.VISIBLE);
                    viewHolder.llRecitemBottomline.setVisibility(View.VISIBLE);
                    viewHolder.tvRecitemNewcommentReplyname1.setText(listener.get(0).getUser_name());
                    viewHolder.tvRecitemNewcommentReplycontent1.setText(listener.get(0).getReply_content());
                    viewHolder.tvRecitemNewcommentReplytime1.setText(listener.get(0).getReply_time());
                    Picasso.with(context)
                            .load("http://siminzhang.top:8088/city_home_server/public/static/posters/" + listener.get(0).getUser_headimage())
                            .into(viewHolder.imgRecitemNewcommentReply1);
                    viewHolder.layoutRecitemNewcommentReply2.setVisibility(View.GONE);
                    viewHolder.tvRecitemNewcommentAllreply.setVisibility(View.GONE);
                } else if (listener.size() >= 2) {
                    viewHolder.layoutRecitemNewcommentReply1.setVisibility(View.VISIBLE);
                    viewHolder.layoutRecitemNewcommentReply2.setVisibility(View.VISIBLE);
                    viewHolder.llRecitemBottomline.setVisibility(View.VISIBLE);
                    viewHolder.tvRecitemNewcommentReplyname1.setText(listener.get(0).getUser_name());
                    viewHolder.tvRecitemNewcommentReplycontent1.setText(listener.get(0).getReply_content());
                    viewHolder.tvRecitemNewcommentReplytime1.setText(listener.get(0).getReply_time());
                    viewHolder.tvRecitemNewcommentReplyname2.setText(listener.get(1).getUser_name());
                    viewHolder.tvRecitemNewcommentReplycontent2.setText(listener.get(1).getReply_content());
                    viewHolder.tvRecitemNewcommentReplytime2.setText(listener.get(1).getReply_time());
                    Picasso.with(context)
                            .load("http://siminzhang.top:8088/city_home_server/public/static/posters/" + listener.get(0).getUser_headimage())
                            .into(viewHolder.imgRecitemNewcommentReply1);
                    Picasso.with(context)
                            .load("http://siminzhang.top:8088/city_home_server/public/static/posters/" + listener.get(1).getUser_headimage())
                            .into(viewHolder.imgRecitemNewcommentReply2);
                    if (listener.size() == 2) {
                        viewHolder.tvRecitemNewcommentAllreply.setVisibility(View.INVISIBLE);
                    } else {
                        viewHolder.tvRecitemNewcommentAllreply.setVisibility(View.VISIBLE);
                        viewHolder.tvRecitemNewcommentAllreply.setText("查看全部" + listener.size() + "条评论");
                    }
                }
                viewHolder.tvRecitemNewcommentAllreply.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, NewsReplyActivity.class);
                        intent.putExtra("id", bean.getComment_id());
                        intent.putExtra("authorid",bean.getUser_id());
                        intent.putExtra("content", bean.getComment_content());
                        intent.putExtra("img", bean.getUser_headimage());
                        intent.putExtra("username", bean.getUser_name());
                        intent.putExtra("time", bean.getComment_time());
                        intent.putExtra("like", bean.getComment_like());
                        intent.putExtra("newsid",bean.getNews_id());
                        context.startActivity(intent);
                    }
                });
                viewHolder.imgRecitemNewcommentComment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog = BottomDialog.create(fragmentManager);
                        dialog.setViewListener(new BottomDialog.ViewListener() {
                            @Override
                            public void bindView(final View v) {
                                final EditText mEditText = (EditText) v.findViewById(R.id.edittext_dialog_newscomment_content);
                                Button button = (Button) v.findViewById(R.id.btn_dialog_newscomment_send);
                                mEditText.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        InputMethodManager imm =
                                                (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                                        imm.showSoftInput(mEditText, 0);
                                    }
                                });
                                button.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        if (!mEditText.getText().toString().equals("")) {
                                            dialog.dismiss();
                                            model.replynewcomment(bean.getNews_id(), bean.getComment_id(), BaseActivity.getUserinfo().getUserid(), mEditText.getText().toString(), replyretrofitListener);
                                        } else {
                                            UIUtils.showToast(context, "格式非法", UIUtils.LENGTH_SHORT);
                                        }
                                    }
                                });
                            }
                        })
                                .setLayoutRes(R.layout.newscomment_dialog)
                                .setDimAmount(0.1f)
                                .setCancelOutside(true)
                                .setTag("评论")
                                .show();
                    }
                });

                viewHolder.imgRecitemNewcommentReply1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, UserInfoActivity.class);
                        intent.putExtra("id",listener.get(0).getUser_id());
                        context.startActivity(intent);
                    }
                });

                viewHolder.imgRecitemNewcommentReply2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, UserInfoActivity.class);
                        intent.putExtra("id",listener.get(1).getUser_id());
                        context.startActivity(intent);
                    }
                });
            }

            @Override
            public void onFail(String msg) {

            }
        };

        RetrofitListener<Down_Universe> islikeretrofitListener = new RetrofitListener<Down_Universe>() {
            @Override
            public void onResponse(Down_Universe listener) {
                if (listener.getMessage().equals("success")) {
                    viewHolder.imgRecitemNewcommentLike.setImageResource(R.drawable.ic_like);
                    flag.add(1);
                } else {
                    flag.add(0);
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };


        final RetrofitListener<Down_Universe> unlikeretrofitListener = new RetrofitListener<Down_Universe>() {
            @Override
            public void onResponse(Down_Universe listener) {
                if (listener.getMessage().equals("success")) {
                    viewHolder.imgRecitemNewcommentLike.setImageResource(R.drawable.ic_like_no);
                    flag.set(position, 0);
                    UIUtils.showToast(context, "取消成功", UIUtils.LENGTH_SHORT);
                    if (bean.getComment_like() == 0 || bean.getComment_like() == 1) {
                        viewHolder.tvRecitemNewcommentlike.setVisibility(View.INVISIBLE);
                    } else {
                        viewHolder.tvRecitemNewcommentlike.setText("" + (bean.getComment_like() - 1));
                    }
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        final RetrofitListener<Down_Universe> likeretrofitListener = new RetrofitListener<Down_Universe>() {
            @Override
            public void onResponse(Down_Universe listener) {
                if (listener.getMessage().equals("success")) {
                    viewHolder.imgRecitemNewcommentLike.setImageResource(R.drawable.ic_like);
                    flag.set(position, 1);
                    UIUtils.showToast(context, "点赞成功", UIUtils.LENGTH_SHORT);
                    if (bean.getComment_like() == 0) {
                        viewHolder.tvRecitemNewcommentlike.setVisibility(View.VISIBLE);
                    }
                    viewHolder.tvRecitemNewcommentlike.setText("" + (bean.getComment_like() + 1));
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };
        model.getReplyNewComment(bean.getComment_id(), listRetrofitListener);
        model.islikenewcomment(BaseActivity.getUserinfo().getUserid(), bean.getComment_id(), islikeretrofitListener);

        viewHolder.imgRecitemNewcommentLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (flag.get(position) == 1) {
                    bean.setComment_like(list.get(position).getComment_like()-1);
                    model.unlikenewcomment(BaseActivity.getUserinfo().getUserid(), bean.getComment_id(), bean.getNews_id(), unlikeretrofitListener);
                } else {
                    bean.setComment_like(list.get(position).getComment_like()+1);
                    model.likenewcomment(BaseActivity.getUserinfo().getUserid(), bean.getComment_id(), bean.getNews_id(), likeretrofitListener);
                }
            }
        });

        viewHolder.imgRecitemNewcomment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra("id",bean.getUser_id());
                context.startActivity(intent);
            }
        });


    }

    RetrofitListener<Down_Universe> replyretrofitListener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if (listener.getMessage().equals("success")) {
                UIUtils.showToast(context, "回复成功", UIUtils.LENGTH_SHORT);
                model.getNewsComment(newsid, commentlistretrofitListener);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    RetrofitListener<List<Down_NewsComment>> commentlistretrofitListener = new RetrofitListener<List<Down_NewsComment>>() {
        @Override
        public void onResponse(List<Down_NewsComment> listener) {
            if (getItemCount() != 0) {
                clearList();
            }
            setList(listener);
            notifyDataSetChanged();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.img_recitem_newcomment_reply2)
        MyCircleImageView imgRecitemNewcommentReply2;
        @BindView(R.id.tv_recitem_newcomment_replyname1)
        TextView tvRecitemNewcommentReplyname1;
        @BindView(R.id.tv_recitem_newcomment_replycontent2)
        TextView tvRecitemNewcommentReplycontent2;
        @BindView(R.id.tv_recitem_newcomment_replytime2)
        TextView tvRecitemNewcommentReplytime2;
        @BindView(R.id.layout_recitem_newcomment_reply2)
        ConstraintLayout layoutRecitemNewcommentReply2;
        @BindView(R.id.img_recitem_newcomment)
        MyCircleImageView imgRecitemNewcomment;
        @BindView(R.id.tv_recitem_newcommentname)
        TextView tvRecitemNewcommentname;
        @BindView(R.id.tv_recitem_newcommentcontent)
        TextView tvRecitemNewcommentcontent;
        @BindView(R.id.tv_recitem_newcommenttime)
        TextView tvRecitemNewcommenttime;
        @BindView(R.id.tv_recitem_newcommentlike)
        TextView tvRecitemNewcommentlike;
        @BindView(R.id.img_recitem_newcomment_like)
        ImageView imgRecitemNewcommentLike;
        @BindView(R.id.img_recitem_newcomment_comment)
        ImageView imgRecitemNewcommentComment;
        @BindView(R.id.linearLayout2)
        LinearLayout linearLayout2;
        @BindView(R.id.img_recitem_newcomment_reply1)
        MyCircleImageView imgRecitemNewcommentReply1;
        @BindView(R.id.tv_recitem_newcomment_replyname2)
        TextView tvRecitemNewcommentReplyname2;
        @BindView(R.id.tv_recitem_newcomment_replycontent1)
        TextView tvRecitemNewcommentReplycontent1;
        @BindView(R.id.tv_recitem_newcomment_replytime1)
        TextView tvRecitemNewcommentReplytime1;
        @BindView(R.id.layout_recitem_newcomment_reply1)
        ConstraintLayout layoutRecitemNewcommentReply1;
        @BindView(R.id.tv_recitem_newcomment_allreply)
        TextView tvRecitemNewcommentAllreply;
        @BindView(R.id.ll_recitem_bottomline)
        LinearLayout llRecitemBottomline;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
